import java.util.*;

public class Test1 {
    public static void main(String[] args) {
        String[] words={"i","love","leetcode","i","love","coding"};
        System.out.println(topKFrequent(words, 3));

    }
    public static List<String> topKFrequent(String[] words, int k) {
        Map<String , Integer> map = new HashMap<>() ;
        for(String str : words){
            if(map.containsKey(str)){
                int val = map.get(str);
                map.put(str,val+1);
            }else{
                map.put(str,1);
            }
        }
        List<String> list = new ArrayList<>() ;
        for(int i =0 ;i < k ; i++){
            String str = words[0] ;
            int max = map.get(str) ;
            for(int j = 1 ; j < words.length ; j ++){
                int val = map.get(words[j]);
                if(max < val ){
                    str = words[j];
                    max = val;
                }else if(max == val && str.compareTo(words[j])>0){
                    str = words[j];
                    max = val;
                }
            }
            list.add(str);
            map.put(str,0);
        }
        return list ;


    }

}

class Student{
    String name ;

    public Student(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
